package com.jh3502.helper.function.person.personlist;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh3502.helper.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PersonListCheckAdapter extends RecyclerView.Adapter<PersonListCheckAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {
    Context mContext;

    private List<PersonListBean> dataList;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null;
    private ButtonInterface viewClickCheck;

    public PersonListCheckAdapter(Context context, List<PersonListBean> dataList) {
        this.dataList = dataList;
        this.mContext = context;
    }

    public void setButtonOnclickCheck(ButtonInterface buttonInterface) {
        this.viewClickCheck = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_person_check, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        //将创建的View注册点击事件
        convertView.setOnClickListener(this);
        convertView.setOnLongClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position + "");
        PersonListBean personMap = dataList.get(position);
        if (personMap == null) {
            return;
        }
        // 进行分组, 比较上一个拼音的首字母和自己是否一致, 如果不一致, 就显示tv_index
        String currentLetter = personMap.getNamePinyin().charAt(0) + "";
        String indexStr = null;
        if (position == 0) {
            // 1. 如果是第一位
            indexStr = currentLetter;
        } else {
            // 获取上一个拼音
            String preLetter = dataList.get(position - 1).getNamePinyin().charAt(0) + "";
            if (!TextUtils.equals(currentLetter, preLetter)) {
                // 2. 当跟上一个不同时, 赋值, 显示
                indexStr = currentLetter;
            }
        }
        viewHolder.itemPersonCheckPinyin.setVisibility(indexStr == null ? View.GONE : View.VISIBLE);
        viewHolder.itemPersonCheckPinyin.setText(indexStr);
        viewHolder.itemPersonCheckName.setText(personMap.getName());
        if (personMap.getSex() == 0) {
            viewHolder.itemPersonCheckSex.setText("男");
        } else {
            viewHolder.itemPersonCheckSex.setText("女");
        }
        if (personMap.isCheck()) {
            Glide.with(mContext).load(R.drawable.ic_flag_red_24dp).into(viewHolder.itemPersonCheckCheck);
        } else {
            Glide.with(mContext).load(R.drawable.ic_flag_gray_24dp).into(viewHolder.itemPersonCheckCheck);
        }
        if (personMap.getLtDataDO() == null) {
            viewHolder.itemPersonCheckTall.setText("");
            viewHolder.itemPersonCheckHead.setText("");
            viewHolder.itemPersonCheckNeck.setText("");
            viewHolder.itemPersonCheckChest.setText("");
            viewHolder.itemPersonCheckShouder.setText("");
            viewHolder.itemPersonCheckArm.setText("");
            viewHolder.itemPersonCheckLeg.setText("");
            viewHolder.itemPersonCheckWaist.setText("");
            viewHolder.itemPersonCheckAss.setText("");
            viewHolder.itemPersonCheckNumber1.setText("");
            viewHolder.itemPersonCheckNumber2.setText("");
            return;
        }
        viewHolder.itemPersonCheckTall.setText(personMap.getLtDataDO().getTall());
        viewHolder.itemPersonCheckHead.setText(personMap.getLtDataDO().getHead());
        viewHolder.itemPersonCheckNeck.setText(personMap.getLtDataDO().getNeck());
        viewHolder.itemPersonCheckChest.setText(personMap.getLtDataDO().getChest());
        viewHolder.itemPersonCheckShouder.setText(personMap.getLtDataDO().getShouder());
        viewHolder.itemPersonCheckArm.setText(personMap.getLtDataDO().getArm());
        viewHolder.itemPersonCheckLeg.setText(personMap.getLtDataDO().getLeg());
        viewHolder.itemPersonCheckWaist.setText(personMap.getLtDataDO().getWaist());
        viewHolder.itemPersonCheckAss.setText(personMap.getLtDataDO().getAss());
        viewHolder.itemPersonCheckNumber1.setText(personMap.getLtDataDO().getUpdress());
        viewHolder.itemPersonCheckNumber2.setText(personMap.getLtDataDO().getDowndress());


        viewHolder.itemPersonCheckCheck.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickCheck != null) {
                //接口实例化后的而对象，调用重写后的方法
                viewClickCheck.onClick(position);
            }
        });
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(@NonNull View v) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick((String) v.getTag());
        }
        return true;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(String s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_person_check_pinyin)
        TextView itemPersonCheckPinyin;
        @BindView(R.id.item_person_check_name)
        TextView itemPersonCheckName;
        @BindView(R.id.item_person_check_sex)
        TextView itemPersonCheckSex;
        @BindView(R.id.item_person_check_tall)
        TextView itemPersonCheckTall;
        @BindView(R.id.item_person_check_head)
        TextView itemPersonCheckHead;
        @BindView(R.id.item_person_check_neck)
        TextView itemPersonCheckNeck;
        @BindView(R.id.item_person_check_chest)
        TextView itemPersonCheckChest;
        @BindView(R.id.item_person_check_shouder)
        TextView itemPersonCheckShouder;
        @BindView(R.id.item_person_check_arm)
        TextView itemPersonCheckArm;
        @BindView(R.id.item_person_check_number1)
        TextView itemPersonCheckNumber1;
        @BindView(R.id.item_person_check_leg)
        TextView itemPersonCheckLeg;
        @BindView(R.id.item_person_check_waist)
        TextView itemPersonCheckWaist;
        @BindView(R.id.item_person_check_ass)
        TextView itemPersonCheckAss;
        @BindView(R.id.item_person_check_number2)
        TextView itemPersonCheckNumber2;
        @BindView(R.id.item_person_check_other)
        ImageView itemPersonCheckOther;
        @BindView(R.id.item_person_check_check)
        ImageView itemPersonCheckCheck;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}